//
//  ZCScanProblemVC.m
//  zctx
//
//  Created by apple on 16/3/17.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCScanProblemVC.h"
#import "ZCScanFootView.h"

#define TABLEVIEWMARGE 19

#import <QuartzCore/QuartzCore.h>

@interface ZCScanProblemVC ()<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate>

@property (nonatomic,strong)UITableView *tableViews;

@property (nonatomic,strong)UIImageView *headImgView;

@property (nonatomic,strong)UILabel *titleLable;



@property (nonatomic,strong)NSString *toBody;

@end

@implementation ZCScanProblemVC


//-(void)dealloc{
//    [self.webViews.scrollView removeObserver:self
//                                  forKeyPath:@"contentSize" context:nil];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    
    self.toBody = [NSString stringWithFormat:@"%@",[self.dataproblemDict objectForKey:@"content"]];
    
    if ([NSString isNull:self.toBody] ==NO) {
//        [self.view addSubview:self.webViews];
        [self.view addSubview:self.tableViews];
        [self WebViewBodys:self.toBody];
        
    }else{
        ZCScanFootView *footView = [[ZCScanFootView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
        [self.view addSubview:footView];
    }
    
//    [self.webViews.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentSize"]) {
      CGFloat  webViewHeight = [[self.webViews stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
        CGRect newFrame       = self.webViews.frame;
        newFrame.size.height  = webViewHeight +20;
        newFrame.size.width = SCREEN_WIDTH - 38;
        
//        NSLog(@"hhh = %f",webViewHeight);
        self.webViews.frame = newFrame;
        [self.tableViews reloadData];
    }
}

-(void)WebViewBodys:(NSString *)str{
    
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]};
    
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NSData dataWithData:[str dataUsingEncoding:NSUTF8StringEncoding]] options:options documentAttributes:nil error:nil];
    
    NSString *resplacString = [NSString stringWithFormat:@"%@%@",@"src=\"",[NetworkManager strURLBasic]];
    //            NSLog(@"resplacString = %@",resplacString);
    NSString *strHtml = [attrStr.string stringByReplacingCharactersInRangeString:attrStr.string andString:@"src=\"" andorString:resplacString];
    
    NSString *string = [NSString showInWebView:strHtml];
    
    NSLog(@"self.toBody2222 = %@",string);
    [self.webViews loadHTMLString:string baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell ==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.tableViews.frame.size.width,44)];
    headView.backgroundColor = [UIColor whiteColor];
    headView.layer.cornerRadius = 5;
    
    self.titleLable.text = [NSString stringWithFormat:@"%@",[self.dataproblemDict objectForKey:@"title"]];
    
    [headView addSubview:self.titleLable];
    self.titleLable.frame = CGRectMake(0, 0, 200, 25);
    [self.titleLable sizeToFit];
    self.titleLable.center = CGPointMake(self.tableViews.frame.size.width/2,22) ;
    CGFloat titleW = self.titleLable.frame.size.width;
    
    if (IS_IPHONE_4_OR_LESS ||IS_IPHONE_5) {
        if (titleW>140) {
            CGRect frame = self.titleLable.frame;
            frame.size.width = 140;
            self.titleLable.frame = frame;
            self.titleLable.center = CGPointMake(self.tableViews.frame.size.width/2,22) ;
        }
    }else{
        if (titleW>200) {
            CGRect frame = self.titleLable.frame;
            frame.size.width = 200;
            self.titleLable.frame = frame;
            self.titleLable.center = CGPointMake(self.tableViews.frame.size.width/2,22) ;
        }
    }
    [headView addSubview:self.headImgView];
    [self.headImgView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headView).with.offset(0);
        make.top.equalTo(headView).with.offset(10);
        make.height.mas_equalTo(25);
        make.left.equalTo(self.titleLable.left).with.offset(-30);
    }];
    [headView bringSubviewToFront:self.titleLable];
    
    UIView *views = [[UIView alloc]initWithFrame:CGRectMake(TABLEVIEWMARGE,43,self.tableViews.frame.size.width - 2 * TABLEVIEWMARGE, 1)];
    [headView addSubview:views];
    [UIView drawDashLine:views lineLength:3 lineSpacing:3 lineColor:[UIColor colorWithHexString:@"#FFD9D4"]];
    
    return headView;

}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH-2 *TABLEVIEWMARGE, self.webViews.frame.size.height)];
    
    [footer addSubview:self.webViews];
    self.webViews.frame = CGRectMake(0, 0,footer.frame.size.width, footer.frame.size.height);
    return footer;
}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return self.webViews.frame.size.height;
}

#pragma mark webView 加载完成
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    if (self.webViews.isLoading) {
        return;
    }
    
    CGRect frame = self.webViews.frame;
    CGFloat height = [[self.webViews stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"]floatValue];
//    CGSize fittingSize = [self.webViews sizeThatFits:CGSizeZero];
    frame.size.height = height;
    self.webViews.frame = frame;
  
    NSLog(@"height 1133=%f = %f ----%f",self.webViews.frame.origin.y,self.webViews.frame.size.height,self.webViews.scrollView.contentSize.height);
    [self.tableViews reloadData];
    
}

-(UITableView *)tableViews{
    if (!_tableViews) {
        _tableViews = [[UITableView alloc]initWithFrame:CGRectMake(TABLEVIEWMARGE, 23, SCREEN_WIDTH - 2 *TABLEVIEWMARGE,SCREEN_HEIGHT-130 -64-23-20) style:UITableViewStyleGrouped];
        _tableViews.dataSource = self;
        _tableViews.delegate = self;
        _tableViews.clipsToBounds = NO;
        
        _tableViews.layer.shadowColor = [UIColor colorWithHexString:@"#748080"].CGColor;
        _tableViews.layer.cornerRadius = 5;
        _tableViews.layer.shadowRadius = 1;
        _tableViews.layer.shadowOpacity = 0.5; //设置阴影的不透明度
        _tableViews.layer.shadowOffset = CGSizeMake(0, 1);
        _tableViews.backgroundColor = [UIColor whiteColor];
        _tableViews.bounces = NO;

    }
    return _tableViews;
}



-(UIImageView *)headImgView{
    if (!_headImgView) {
        _headImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
        UIImage *image = [UIImage imageNamed:@"scan_red"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 25, 0, 25)];
        _headImgView.image = image;
    }
    return _headImgView;
}

-(UILabel *)titleLable{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc]initWithFrame:CGRectZero];
        _titleLable.text = @"谷物";
        _titleLable.font = [UIFont systemFontOfSize:16];
        _titleLable.textColor = [UIColor whiteColor];
        
        _titleLable.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLable;
}

-(UIWebView *)webViews{
    if (!_webViews) {
        _webViews = [[UIWebView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,500)];
        _webViews.delegate = self;
        _webViews.scrollView.bounces = NO;
        _webViews.scrollView.scrollEnabled = NO;
        _webViews.tag = 3;
    }
    return _webViews;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
